<?

# Kapitelnummerierung :-)
#
# #snum enthaelt eine Nummer der Form x.y.z, wobei es beliebig viele Abschnitte
# geben kann.
#
# #level enthaelt den Level, also welche der Teilnummern veraendert werden soll.
#
# #dir enthaelt die Richtung in die veraendert werden soll, wobei 0 eins nach
# unten und 1 eins nach oben bedeutet. -1 bedeutet alle Ziffern groesser
# #level wegschneiden und die letzte Nummer erhoehen. -2 tut das selbe,
# reduziert jedoch die letzte Nummer. -3 bedeutet die Nummern gleich lassen,
# aber die Anzahl der Elemente an #level anpassen.
#
# #level zaehlt von 0 weg!
#
function kNumber($snum, $level, $dir) {
	$anz = str_wordcount($snum);
	$kpnum = $snum;

	# Haben wir weniger Einzelnummern im String, haengen wir ".1" so
	# lange an, bis wir #level erreicht haben.
	if ($anz <= $level) {
	   while ($anz <= $level) {
	      $kpnum .= ".1";
	      $anz++;
	   }

	   if ($dir == -3) {
	      return $kpnum;
	   }
	}

	$anz = $level + 1;
	$anum = explode(".", $kpnum);

	if ($dir < 0) {
	   if ($dir == -1) {
	      $anum[$level]++;
	   } else if ($dir == -2) {
	      $anum[$level]--;
	   }
	} else {
	   if ($dir == 0) {
	      $anum[$level]++;
	   } else {
	      $anum[$level]--;
	   }
	}

	$num = "";

	for ($i = 0; $i < $anz; $i++) {
	   if ($i > 0) {
	      $num .= "." . (string)$anum[$i];
	   } else {
	      $num = (string)$anum[$i];
	   }
	}

	return $num;
}

function SelectTask($pl_num, $id, $sel=0) {
	global $phase;
	global $numphase;

	$lodb = OpenDB();
	$loquery = "select ta_num, ta_name, ta_id, ta_level, ta_meeting,";
	$loquery .= "ta_phase from task where ta_plnum = $pl_num order by ta_id";
	$loresult = QueryDB($lodb, $loquery);

	if (!$loresult) {
	   closeDB($lodb);
	   return false;
	}

	$numrows = numrowsDB($loresult);
	$row = 0;
	$snum = "1";
	$olevel = 0;

	while ($row < $numrows) {
	   $data = fetchDB($loresult, $row);
	   $ta_num = $data[0];
	   $ta_name = $data[1];
	   $ta_id = $data[2];
	   $ta_level = $data[3];
	   $ta_meeting = $data[4];
	   $tphase = $data[5];

	   $loquery = "select ta_level from task where ";
	   $loquery .= "ta_plnum = $pl_num and ta_id > $ta_id order by ta_id";
	   $loresult2 = QueryDB($lodb, $loquery);

	   if (!$loresult2) {
	      closeDB($lodb);
	      return false;
	   }

	   $nr = numrowsDB($loresult2);

	   if ($nr > 0) {
	      $data = fetchDB($loresult2, 0);
	      $xlevel = $data[0];
	   } else {
	      $xlevel = $ta_level;
	   }

	   if ($olevel == 0) {
	      $level = 0;
	      $olevel = $ta_level;
	      $dir = -3;
	   } else if ($olevel < $ta_level) {
	      $level = $ta_level - 1;
	      $olevel = $ta_level;
	      $dir = -3;
	   } else if ($olevel == $ta_level) {
	      $level = $ta_level - 1;
	      $dir = 0;
	   } else if ($olevel > $ta_level) {
	      $level = $ta_level - 1;
	      $olevel = $ta_level;
	      $dir = -1;
	   }

	   $snum = kNumber ($snum, $level, $dir);

	   if ($phase == 1 && $numphase == 1 && isset($tphase)) {
	      $p = strpos($snum, ".");

	      if ($p === false)
	         $rest = "";
	      else
	         $rest = substr($snum, $p);

	      $snum = (string)$tphase . $rest;
	   }

	   if (!$ta_meeting && $ta_level > 1 && $xlevel <= $ta_level) {
	      if (!$id) {
	         echo "<option value=\"$ta_num\"";
		 $comp = $ta_num;
	      } else {
	         echo "<option value=\"$ta_id\"";
		 $comp = $ta_id;
	      }

	      if ($comp == $sel)
	         echo "selected";

	      echo ">$snum $ta_name</option>\n";
	   }

	   $row++;
	}

//	closeDB($lodb);
	return true;
}

function GetTaskNum($pl_num, $ta_id, $ta_level, $ta_phase, $ta_meeting, $snum, $typ, $db=-1) {
	global $phase;
	global $numphase;

	if (strlen($snum) <= 0)
	   $snum = "1";

	if ($db == -1)
	   $dbs = OpenDB();
	else
	   $dbs = $db;

	# Finde den naechsten Level
	$query = "select ta_level from task where ta_plnum = $pl_num and ";
	$query .= "ta_id > $ta_id order by ta_id";
	$result = QueryDB($dbs, $query);

	if (!$result) {
	   return false;
	}

	$numrows = numrowsDB($result);

	if ($numrows > 0) {
	   $data = fetchDB($result, 0);
	   $xlevel = $data[0];
	} else {
	   $xlevel = $ta_level;
	}

	# Finde den vorhergehenden Level
	$query = "select ta_level from task where ta_plnum = $pl_num and ";
	$query .= "ta_id < $ta_id order by ta_id desc";
	$result = QueryDB($dbs, $query);

	if (!$result) {
	   return false;
	}

	$numrows = numrowsDB($result);

	if ($numrows > 0) {
	   $data = fetchDB($result, 0);
	   $olevel = $data[0];
	} else {
	   $olevel = 0;
	}


	if ($olevel == 0) {
	   $level = 0;
	   $dir = -3;
	} else if ($olevel < $ta_level) {
	   $level = $ta_level - 1;
	   $dir = -3;
	} else if ($olevel == $ta_level) {
	   $level = $ta_level - 1;
	   $dir = 0;
	} else if ($olevel > $ta_level) {
	   $level = $ta_level - 1;
	   $dir = -1;
	}

	$kpnum = kNumber ($snum, $level, $dir);

	if ($phase == 1 && $numphase == 1 && isset($ta_phase) && $ta_phase >= 0) {
	   $p = strpos($kpnum, ".");

	   if ($p === false)
	      $rest = "";
	   else
	      $rest = substr($kpnum, $p);

	   $kpnum = (string)$ta_phase . $rest;
	}

	if (!$ta_meeting && $ta_level > 1 && $xlevel <= $ta_level)
	   $typ = true;
	else
	   $typ = false;

	if ($db == -1)
	   closeDB($dbs);

	return $kpnum;
}

?>
